package com.easonji.admin.manager.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.easonji.admin.manager.DeptManager;
import com.easonji.admin.mapper.DeptMapper;
import com.easonji.admin.model.Dept;
import com.easonji.common.constant.cache.CacheConsts;
import com.easonji.common.constant.cache.CacheKeyConsts;
import com.easonji.common.util.StringUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: DeptManager
 * @Description: 部门管理通用逻辑
 * @Author: LiuJun
 * @date: 2018/07/23 12:50
 * @Version: 1.0
 * @Copyright: Copyright (c) 2018 - 2019
 */
@Service
public class DeptManagerImpl implements DeptManager {

    @Resource
    private DeptMapper deptMapper;

    @Override
    @Cacheable(value = CacheConsts.CONSTS, key = "'" + CacheKeyConsts.DEPT_NAME + "'+#deptId")
    public String getDeptNameById(Integer deptId) {
        String result = "";
        Dept dept = deptMapper.selectById(deptId);
        if (StringUtils.isNotEmpty(dept)) {
            result = dept.getName();
        }
        return result;
    }

    @Override
    @Cacheable(value = CacheConsts.CONSTS, key = "'" + CacheKeyConsts.DEPT_SUB_ID + "'+#deptId")
    public List<Integer> querySubDeptIdListById(Integer deptId) {
        List<Integer> result = new ArrayList<>();
        Wrapper<Dept> wrapper = new EntityWrapper<>();
        wrapper = wrapper.like("all_parent_id", "[" + deptId + "]");
        List<Dept> depts = deptMapper.selectList(wrapper);
        for (Dept dept : depts) {
            result.add(dept.getId());
        }
        return result;
    }
}
